package com.linewell.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linewell.system.dao.ISysRolesDao;
import com.linewell.system.model.SysRoles;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.service.ISysRolesService;
import com.linewell.system.utils.HqlHelper;

@Service
public class SysRolesServiceImpl implements ISysRolesService {
	@Autowired
	private ISysRolesDao sysRolesDao;

	@Override
	public boolean save(SysRoles sysRoles) {
		// TODO Auto-generated method stub
		sysRolesDao.save(sysRoles);

		return true;
	}

	@Override
	public List<SysRoles> findAll() {
		// TODO Auto-generated method stub
		return sysRolesDao.find("FROM SysRoles");
	}

	@Override
	public boolean deleteAll(String ids) {
		// TODO Auto-generated method stub
		sysRolesDao.deleteAll(ids);
		return true;
	}

	@Override
	public boolean update(SysRoles sysRoles) {
		// TODO Auto-generated method stub
		sysRolesDao.updateAll(sysRoles);
		;
		return true;
	}

	@Override
	public DataGrid datagrid(PageHelper ph, SysRoles sysRoles,Integer depId) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		List<SysRoles> cml = new ArrayList<SysRoles>();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();

		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysRoles t where 1=1 ");
		if(depId != null && !"".equals(depId)){
			hql.append("  AND FIND_IN_SET(t.sysDepartment.depId,getChildDepId("+depId+")) > 0 ");
		    }
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysRoles> l = sysRolesDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysRolesDao.count("select count(*) " + hql, params));

		return dg;
	}

	@Override
	public void removeAll(String ids) {
		// TODO Auto-generated method stub
		sysRolesDao.removeAll(ids);

	}

	@Override
	public List<SysRoles> getUser(String id) {
		// TODO Auto-generated method stub

		String hql = "from SysRoles s where s.id=" + id;
		List<SysRoles> c = sysRolesDao.find(hql);
		return c;
	}

	@Override
	public SysRoles getRoleMenu(Integer gid) {
		String hql = "from SysRoles s where s.id=" + gid;
		return sysRolesDao.find(hql).get(0);
	}

	@Override
	public boolean saveAuthority(String mids, Integer gid) {
		// TODO Auto-generated method stub
		return sysRolesDao.saveAuthority(mids,gid);
	}

	@Override
	public List<SysRoles> getRoles(Integer depId){
		// TODO Auto-generated method stub
		return sysRolesDao.getRoles(depId);
	}
}
